package com.hengpeng.itfints.controller.member;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.area.AreaCache;
import com.hengpeng.common.area.vo.Area;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.util.Validate;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.persist.UserBankCard;
import com.hengpeng.itfinbase.persist.UserBankCard.BankType;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfints.controller.BaseController;

/**
 * <pre>
 * 银行卡管理类
 * </pre>
 * @author shanguogang
 * @version 1.0, 2015-8-6
 */
@Controller
@RequestMapping("/memberCenter")
public class UserBankCardController extends BaseController
{
	/**
	 * <pre>
	 * 添加银行卡跳转
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addUserBankCard")
	public String addUserBankCard(ModelMap map,HttpServletRequest request){
		List<Area> proviceList = AreaCache.getInstance().getAllProvince();
		map.put("proviceList", proviceList);
		map.put("banks",UserBankCard.BankType.values());
		return "/page/account/card/cardInfo.jsp";
	}
	
	/**
	 * <pre>
	 * 添加银行卡
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveUserBankCard")
	@ResponseBody
	public ServiceResult<String> saveUserBankCard(ModelMap map,HttpServletRequest request,String bankCardNo,String bankname,String provinceId,String city,String isPriority,String subbranch,String realName){
		ServiceResult<String> result = new ServiceResult<String>();
		if(Validate.validateObjectsNullOrEmpty(realName)){
			return validateFailure("请先实名认证");
		}
		if (Validate.validateObjectsNullOrEmpty(realName, bankCardNo, bankname, provinceId, city, subbranch))
			return validateFailure("参数为空");
		UserInfo m = getSessionMember(request).getUser();
		UserBankCard card=new UserBankCard();
		card.setBankCardNo(bankCardNo);
		card.setBankName(BankType.valueOf(bankname));
		card.setCity(city);
		card.setProvince(AreaCache.getInstance().get(Long.parseLong(provinceId)).getName());
		card.setIsPriority(BoolValue.valueOf(isPriority));
		card.setRealName(m.getRealName());
		card.setSubbranch(subbranch);
		card.setUserName(m.getUserName());
		ResponseMessage mesg=serviceFactory.getUserBankCardService().saveUserBankCard(card);
		if(mesg!=null){
			if (RespCodeConstants.CommonCode.TRANS_SUC.equals(mesg.getRespCode()))
			{
				result.setIsSuccess(true);
				result.setMessage("添加银行卡成功");
				refreshSessionMember(request);
			}
			else
			{
				return validateFailure(mesg.getRespDesc());
			}
		}else{
			return validateFailure("添加银行卡失败");
		}
		return result;
	}
	
	/**
	 * <pre>
	 * 修改银行卡
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/upBankCard/{id}")
	public String upBankCard(ModelMap map,HttpServletRequest request,@PathVariable Long id){
		UserInfo m = getSessionMember(request).getUser();
		UserBankCard card=serviceFactory.getPersistService().get(UserBankCard.class, id);
		if (card != null && !card.getUserName().equals(m.getUserName()))
		{
			card = null;
		}
		List<Area> proviceList = AreaCache.getInstance().getAllProvince();
		Long userProvinceId = 0L;
		String userProvinceName = "";
		String userCityName = "";
		if (card.getProvince() != null)
		{
			for (Area area : proviceList)
			{
				if (area.getName().equals(card.getProvince()))
				{
					userProvinceId = area.getId();
					userProvinceName = area.getName();
					break;
				}
			}
		}
		Area province = AreaCache.getInstance().get(userProvinceId);
		List<Area> cityList = null;
		if (province != null)
			cityList = province.getChildList();
		map.put("cityList", cityList);
		if (cityList != null)
		{
			for (Area city : cityList)
			{
				if (city.getName().equals(card.getCity()))
				{
					userCityName = city.getName();
					break;
				}
			}
		}
		map.put("userProvince", userProvinceName);
		map.put("userCity", userCityName);
		map.put("proviceList", proviceList);
		map.put("banks",UserBankCard.BankType.values());
		map.put("card", card);
		return "/page/account/card/editCardInfo.jsp";
	}
	
	/**
	 * <pre>
	 * 修改银行卡
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateUserBankCard/{id}")
	@ResponseBody
	public ServiceResult<String> updateUserBankCard(ModelMap map,HttpServletRequest request,
			@PathVariable Long id,String bankCardNo,String bankname,
			String provinceId,String city,String isPriority,String subbranch,String realName){
		ServiceResult<String> result = new ServiceResult<String>();
		if (Validate.validateObjectsNullOrEmpty(realName, bankCardNo, bankname, provinceId, city, subbranch))
			return validateFailure("参数为空");
		UserBankCard card=serviceFactory.getPersistService().get(UserBankCard.class, id);
		if(card==null){
			return validateFailure("找不到该银行卡");
		}
		UserInfo m = getSessionMember(request).getUser();
		card.setBankCardNo(bankCardNo);
		card.setBankName(BankType.valueOf(bankname));
		card.setCity(city);
		card.setProvince(AreaCache.getInstance().get(Long.parseLong(provinceId)).getName());
		card.setIsPriority(BoolValue.valueOf(isPriority));
		card.setRealName(m.getRealName());
		card.setSubbranch(subbranch);
		card.setUserName(m.getUserName());
		ResponseMessage mesg=serviceFactory.getUserBankCardService().updateUserBankCard(card);
		if(mesg!=null){
			if (RespCodeConstants.CommonCode.TRANS_SUC.equals(mesg.getRespCode()))
			{
				result.setIsSuccess(true);
				result.setMessage("修改银行卡成功");
				refreshSessionMember(request);
			}
			else
			{
				return validateFailure(mesg.getRespDesc());
			}
		}else{
			return validateFailure("修改银行卡失败");
		}
		return result;
	}
	
	/**
	 * <pre>
	 * 删除银行卡
	 * </pre>
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delBankCard/{id}")
	@ResponseBody
	public ServiceResult<String> delBankCard(ModelMap map,HttpServletRequest request,@PathVariable Long id){
		ServiceResult<String> result = new ServiceResult<String>();
		UserBankCard card=serviceFactory.getPersistService().get(UserBankCard.class, id);
		if(card==null){
			return validateFailure("找不到该银行卡");
		}
		ResponseMessage mesg=serviceFactory.getUserBankCardService().deleteUserBankCard(card);
		if(mesg!=null){
			if (RespCodeConstants.CommonCode.TRANS_SUC.equals(mesg.getRespCode()))
			{
				result.setIsSuccess(true);
				result.setMessage("删除银行卡成功");
				refreshSessionMember(request);
			}
			else
			{
				return validateFailure(mesg.getRespDesc());
			}
		}else{
			return validateFailure("删除银行卡失败");
		}
		return result;
	}
	
	@RequestMapping(value = "/findArea/{id}")
	@ResponseBody
	public List<Area> findArea(@PathVariable Long id)
	{

		List<Area> citys = new ArrayList<Area>();
		Area area = AreaCache.getInstance().get(id);
		if (area != null)
		{
			citys = area.getChildList();
		}
		return citys;
	}
}
